/*
 * $Id: $
 */
package org.rcgwt.client.ui.grid.internal;

import org.rcgwt.client.ui.grid.IWidgetsRegistry;
import org.rcgwt.client.ui.grid.internal.js.IntMap;

import com.google.gwt.user.client.ui.Panel;
import com.google.gwt.user.client.ui.Widget;

public class WidgetsRegistryImpl implements IWidgetsRegistry {

    private IntMap<Widget> widgetIdToWidget;

    private int widgetIdentifiers = 0;

    private final Panel widgetContainer;

    public WidgetsRegistryImpl(Panel widgetContainer) {
        this.widgetContainer = widgetContainer;

        widgetIdToWidget = new IntMap<Widget>();
    }

    public void remove(int widgetId) {
        Widget widget = widgetIdToWidget.remove(widgetId);
        assert widget != null : "Unknown widget '" + widgetId + "'";

        widgetContainer.remove(widget);
    }

    public void removeAll() {
        // TODO
    }

    public int add(Widget widget) {
        widgetContainer.add(widget);

        int widgetId = ++widgetIdentifiers;

        widgetIdToWidget.put(widgetId, widget);

        return widgetId;
    }

}
